<template>
  <section>
    <div class="visit_detail_box">
      <div class="userinfo p-sm">
        <p>
          <img v-if="basicDetail.head != ''" :src="basicDetail.head | imageUrl" class="patient_img" alt />
          <img v-else src="/img/unknown-avatar.4f0c79ac.png" class="patient_img" alt />
        </p>
        <h3>{{ basicDetail.name }}</h3>
        <div>
          <el-button type="primary" icon="el-icon-document" @click="getRecord()">就诊记录</el-button>
        </div>
        <p>
          <!-- <el-button @click="getRecord()" type="primary">就诊记录</el-button> -->
          <!-- <button type="button" class="btn btn-primary">发券</button> -->
        </p>
        <div class="user-field">
          <span>病历号：</span>
          {{ basicDetail.archivesCode }}
        </div>
        <div class="user-field">
          <span>电话：</span>
          {{ basicDetail.mobile }}
        </div>
        <div class="user-field">
          <span>性别：</span>
          {{ basicDetail.sex | toSex }}
        </div>
        <div class="user-field">
          <span>年龄：</span>
          <label v-if="basicDetail.age > 18">{{ basicDetail.age }}岁</label>
          <label v-else>{{ basicDetail.age }}岁{{ basicDetail.sdfMonth }}月{{ basicDetail.sdfDay }}天</label>
        </div>
        <div v-if="basicDetail" class="user-field">
          <span>余额：</span>
          <span style="color: red">￥ {{ basicDetail.account.amount }}</span>
        </div>
        <div v-if="basicDetail.age < 18 && isBasic == true" style="margin-top: 10px">
          <el-button type="success" plain @click="doPrint()">WHO曲线图</el-button>
        </div>
        <div v-if="basicDetail.age < 18 && isBasic == false" style="margin-top: 10px">
          <el-button type="success" plain @click="getBasic()">儿童基础信息</el-button>
        </div>
        <!-- <log v-model="dialogRecord" @detail="getCase"></log>
        <childBaisc v-if="dialogBasic" v-model="dialogBasic" :detail="detail" :curItem="basicValue" @success="getChildInfo()"></childBaisc>
        <iframe v-if="showReport" frameborder="0" height="0px" width="0px" ref="iframe" src="/print/who.html"></iframe>
        <iframe v-show="showReports" frameborder="0" height="0px" width="0px" ref="iframeReport" src="/print/report.html"></iframe>-->
      </div>
      <div class="user-content p-sm">
        <el-tabs v-model="activeName" type="border-card">
          <el-tab-pane label="基本信息" name="info"></el-tab-pane>
          <el-tab-pane v-if="isChild" label="儿童体检" name="examination"></el-tab-pane>
          <el-tab-pane label="就诊信息" name="case"></el-tab-pane>
          <el-tab-pane label="预约信息" name="order"></el-tab-pane>
          <el-tab-pane label="套餐信息" name="combo"></el-tab-pane>
          <el-tab-pane label="收费信息" name="record"></el-tab-pane>
          <el-tab-pane label="退费信息" name="refundInfo"></el-tab-pane>
          <el-tab-pane label="会员信息" name="vip"></el-tab-pane>
          <el-tab-pane label="积分信息" name="integrate"></el-tab-pane>

          <!-- <el-tab-pane
            v-for="(item,index) in acList"
            :label="item.label"
            :name="item.name"
            :key="index"
          ></el-tab-pane> -->
          <!-- <el-tab-pane label="消费记录" name="record"></el-tab-pane> -->
          <div class="jbxx">
            <comm v-if="active" :is="activeName" :detail="detail" :ispersonal="ispersonal" @edit="editPersonal" @success="onSuccess"></comm>
          </div>
        </el-tabs>
      </div>
      <!-- <div id="myHeight" style="width:200px;height:200px;"></div>
      <div id="chart" style="width:200px;height:200px;" ></div>-->
      <log v-model="dialogRecord" @detail="getCase"></log>
      <childBaisc v-if="dialogBasic" v-model="dialogBasic" :detail="detail" :curItem="basicValue" @success="getChildInfo()"></childBaisc>
      <iframe v-if="showReport" frameborder="0" height="0px" width="0px" ref="iframe" src="/print/who.html"></iframe>
      <iframe v-show="showReports" frameborder="0" height="0px" width="0px" ref="iframeReport" src="/print/report.html"></iframe>
    </div>
  </section>
</template>
<script>
export default {
  name: "userDetail",
  data() {
    return {
      activeName: "info",
      dialogRecord: false,
      dialogBasic: false,
      basicValue: [],
      // acList: [
      //   {
      //     label: "基本信息",
      //     name: "info",
      //   },
      //   {
      //     label: "就诊信息",
      //     name: "case",
      //   },
      //   {
      //     label: "预约信息",
      //     name: "order",
      //   },
      //   {
      //     label: "套餐信息",
      //     name: "combo",
      //   },
      //   {
      //     label: "收费信息",
      //     name: "record",
      //   },
      //   {
      //     label: "会员信息",
      //     name: "vip",
      //   },
      //   {
      //     label: "积分信息",
      //     name: "integrate",
      //   },
      //   // {
      //   //   label: "检查报告",
      //   //   name: "laboratory"
      //   // }
      // ],
      active: false,
      showReport: false,
      showReports: false,
      isBasic: false,
      detail: {
        levelProjectRelation: [],
      },
      basicDetail: "",
      ispersonal: false,
      iframeReport: "",
      printList: [],
      isChild: false,
    };
  },
  activated() {
    const patientId = this.jieMi(this.$route.query.id);
  },
  mounted() {
    this.getInit();
  },
  methods: {
    getInit() {
      this.findUsUserById();
      this.getPatientInfo();
      this.getExaminList();
      this.iframeReport = this.$refs.iframeReport.contentWindow;
      let type = this.$route.query.type;
      if (type) {
        if (type == 3) {
          this.activeName = "case";
        } else if (type == 2) {
          this.activeName = "vip";
        } else {
          this.activeName = "info";
        }
      }
    },
    findUsUserById() {
      let th = this;
      th.showLoading();
      th.verifyUrlAttack(th.$route.query).then(() => {
        let uid = th.jieMi(th.$route.query.id);
        th.$api.getThePatientLibraryById({ patientId: uid, hpId: th.adminUser.hpId }).then((res) => {
          if (res.executed) {
            th.showLoading(false);
            let _detail = {
              levelProjectRelation: [
                { projectName: "辅助检查医嘱", discount: 100 },
                { projectName: "实验室检查医嘱", discount: 100 },
                { projectName: "治疗医嘱", discount: 100 },
                { projectName: "处方医嘱", discount: 100 },
                { projectName: "诊疗费", discount: 100 },
                { projectName: "材料费", discount: 100 },
                { projectName: "其他收费", discount: 100 },
              ],
            };
            _detail = Object.assign(_detail, res.thePatientLibraryDetail);
            if (_detail.labelIds != "" && _detail.labelIds != undefined) {
              _detail.labelIds = _detail.labelIds.split(",");
            }
            th.active = true;
            th.ispersonal = false;
            th.detail = _detail;
            if (_detail.levelId) {
              th.$api
                .getLevelEnumById({
                  id: _detail.levelId,
                  hpId: th.adminUser.hpId,
                })
                .then((res) => {
                  if (res.executed) {
                    (_detail.levelProjectRelation = res.projectRelationLevelList), (th.detail = _detail);
                    // th.levelProjectRelation=res.levelProjectRelation;
                    // th.levelCondition=res.levelCondition;
                  }
                });
            }
            if (th.detail.age < 18) {
              this.isChild = true;
              this.getChildInfo();
            }
          }
        });
      });
    },
    getChildInfo() {
      let th = this;
      //首先判断是否在儿保建档
      th.$api.getArchivesById({ id: th.jieMi(th.$route.query.id) }).then((res) => {
        if (res.executed) {
          if (res.info.valuesInfo.length > 0) {
            th.basicValue = res.info.valuesInfo;
            th.isBasic = true;
          } else {
            if (th.detail.age < 18) {
              th.$confirm(`该儿童暂时未填写基础信息，是否现在填写?`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
                center: true,
              })
                .then(() => {
                  th.dialogBasic = true;
                })
                .catch(() => {});
            }
          }
        } else {
          //1、没建档的情况下在儿保建档
          if (th.detail.age < 18) {
            //没有生日不掉儿保
            if (!th.detail.birthday) return;
            let child = {
              name: th.detail.name,
              cardNo: "-1",
              motherPhone: th.detail.mobile,
              sex: th.detail.sex == 1 ? "MAN" : "WOMEN",
              age: th.detail.birthday.replace(/\-/g, "/"),
              patientId: th.detail.patientId,
              operatorId: th.adminUser.memberId,
            };
            th.$api.insertChildArchives(child).then((res) => {
              if (res.executed) {
                th.$confirm(`该儿童暂时未填写基础信息，是否现在填写?`, "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                  center: true,
                })
                  .then(() => {
                    th.dialogBasic = true;
                  })
                  .catch(() => {});
              }
            });
          }
        }
      });
    },
    //获取体格检查打印项目
    getExaminList() {
      let th = this;
      th.$api.findListByCategoryId({ isOnlyRoot: "T", categoryId: "1000160004" }).then((res) => {
        if (res.executed) {
          let list = res.infos;
          let data = [];
          for (let i = 0; i < list.length; i++) {
            let arr = {};
            Object.assign(arr, list[i]);
            if (arr.attrType == "SELECT") {
              if (arr.attrValue) {
                if (arr.attrValue.length > 0) {
                  arr.values = arr.attrValue[0].valValue;
                }
              }
            } else {
              arr.values = "";
            }
            data.push(arr);
            th.showLoading(false);
          }
          th.printList = data;
        }
      });
    },
    doPrint() {
      let th = this;
      // th.drawLine();
      th.showReport = true;
      th.showLoading();
      th.$api.getChildWho({ patientId: th.detail.patientId, whoType: 1 }).then((res) => {
        if (res.executed) {
          setTimeout(function () {
            th.iframeContent = th.$refs.iframe.contentWindow;
            th.iframeContent.postMessage(
              {
                cmd: "postJson",
                params: JSON.stringify({
                  detail: th.detail,
                  whoInfo: res,
                  // disAmount: th.disAmount,
                  // list: th.itemList,
                  // billPayways:th.billPayways,
                  logo: th.adminUser.printLogo,
                  // orderCode:th.$route.query.orderCode,
                  // total: th.totalMoney,
                  hospitalName: th.adminUser.hospitalName,
                }),
              },
              "*"
            );
          }, 500);
        }
      });
      setTimeout(function () {
        th.showLoading(false);
      }, 3000);
    },
    getCase(type) {
      let th = this;
      if (type) th.activeName = "case";
    },
    //修改个人史显示
    editPersonal() {
      this.ispersonal = !this.ispersonal;
    },
    getRecord() {
      this.dialogRecord = true;
    },
    getBasic() {
      let th = this;
      th.dialogBasic = true;
    },
    toExamin() {
      let th = this;
      th.$router.push({
        path: "/page/childExam",
      });
    },
    async getPatientInfo() {
      await this.$api.getPatientInfo({ patientId: this.jieMi(this.$route.query.id) }).then((res) => {
        if (res.executed) {
          this.basicDetail = res.dataBody;
        }
      });
    },
    onSuccess() {
      this.getPatientInfo();
      this.findUsUserById();
    },
  },
  watch: {
    $route: function () {
      const objs = this.$route.query;
      if (objs.hasOwnProperty("id")) {
        if (this.jieMi(objs.id) != this.basicDetail.patientId) {
          this.getInit();
          this.activeName = "info";
          this.isChild = false;
        }
      }
    },
  },
  components: {
    info: () => import("./_child/info"),
    record: () => import("./_child/record"),
    order: () => import("./_child/order"),
    vip: () => import("./_child/vipV2"),
    integrate: () => import("./_child/integrate"),
    case: () => import("./_child/case"),
    combo: () => import("./_child/combo"),
    examination: () => import("./_child/childExam"),
    // laboratory: () => import("./_child/laboratory"),
    assist: () => import("./_child/assist"),
    log: () => import("./_com/patientRecord"),
    childBaisc: () => import("./_com/editChildBasic"),
    refundInfo: () => import("./_child/refundInfo"),
  },
};
</script>
<style lang="scss" scoped>
@import "./_css/detail.scss";
.patient_img {
  border: 1px solid #ddd;
  border-radius: 5px;
  display: inline-block;
  width: 150px;
  height: 150px;
}
.user-content {
  position: relative;
}
.user_info_return_box {
  display: inline-block;
  position: absolute;
  z-index: 111;
  right: 16px;
  top: 17px;
}
</style>

